package com.vgemv.jsutilitysdkdemo.module.Screen;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewParent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

//import com.vgemv.jsutilitysdk.Display.JsActivityDisplayManager.JsActivityDisplayManager;
//import com.vgemv.jsutilitysdk.Display.JsDisplayStyle;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.JsThread.JsThreadManager;

public class ScreenActivity extends AppCompatActivity {

    private LinearLayout mll_root;

    public Handler mHandler = new Handler(Looper.getMainLooper());

    public void translucentStatusBar(Window window) {
        if(window ==null)return;
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        else if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        translucentStatusBar(getWindow());

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setId(View.generateViewId());
        mll_root = linearLayout;
        setContentView(linearLayout);


        TestView tv = new TestView(this);
        mll_root.addView(tv);



        Rect rootViewFrame = JsGeometryManager.getActivityRootViewFrame(this);
        JsLogManager.logc_d("ScreenActivity","rootViewFrame frame is %s",rootViewFrame.toString());




        JsThreadManager.runInMainThreadDelayed(1000, new Runnable() {
            @Override
            public void run() {
                Rect rect = new Rect();
                mll_root.getGlobalVisibleRect(rect);
                ViewParent vp = mll_root.getParent();
                JsLogManager.logc_d("ScreenActivity","linearlayout frame is %s",rect.toString());
                int screenHeight = JsGeometryManager.getScreenSizeInPixel().getHeight();
                JsLogManager.logc_d("ScreenActivity","screen height is %d",screenHeight);

                //1 每个Activity都有一个PhoneWindow
                Window activityPhoneWindow = getWindow();
//                int activityPhoneWidnow = activityPhoneWindow;

                //2 每个Window都有一个DecorView
                //DecorView全屏 全屏  mContentRoot 是LinearLayout
                View decorView = (View)activityPhoneWindow.getDecorView();
                decorView.getGlobalVisibleRect(rect);
                JsLogManager.logc_d("ScreenActivity","decoor view frame is %s",rect.toString());

                // status
                View statusBar = decorView.findViewById(android.R.id.statusBarBackground);
                if(statusBar != null) {
                    statusBar.setBackgroundColor(Color.RED);
                    statusBar.getGlobalVisibleRect(rect);
                    JsLogManager.logc_d("ScreenActivity", "statusBar frame is %s", rect.toString());
                }

                //navigation
                View navigationBar = decorView.findViewById(android.R.id.navigationBarBackground);
                navigationBar.setBackgroundColor(Color.RED);
                navigationBar.getGlobalVisibleRect(rect);
                JsLogManager.logc_d("ScreenActivity","navigationBar frame is %s",rect.toString());


                //DecorViewContent  全屏 ContentFrameLayout
                final View contentView = decorView.findViewById(android.R.id.content);
                contentView.getGlobalVisibleRect(rect);
                JsLogManager.logc_d("ScreenActivity","content view frame is %s",rect.toString());



                //感觉windowManager得到的height都是不对的.
                Point point = new Point(0,0);
//                getWindowManager().getDefaultDisplay().getSize(point);
                getWindowManager().getDefaultDisplay().getRealSize(point);
                int activityWindowManagerHeight = point.y;
                JsLogManager.logc_d("ScreenActivity","Activity window manager height is %d",activityWindowManagerHeight);

                Rect safeAresFrame = JsGeometryManager.getScreenSafeAreaFrameInPixel();
                JsLogManager.logc_d("ScreenActivity","Screen safe area frame is %s",safeAresFrame.toString());

                int notchHeight = JsGeometryManager.getNotchHeight();
                JsLogManager.logc_d("ScreenActivity","Notch height is %d",notchHeight);
            }
        });

    }
}

/**
 * 全面屏结果:
 * LinearLayout   0,137 - 1440,3144
 * ScreenHeight 3007
 * DecorView     0,0 - 1400,3200
 * ContentView   0,0 - 1400,3200
 *
 * 结果分析:
 * 1,全屏大小是3200,1400    安全区域高度 0-toppadding-137-safearea-3144-botompadding-3200
 * 2,toppadding = 137, bottompadding = 56.  safeares.height = 3007.
 * 2,ScreenHeight得到的并不准确,只是中间安全区域的高度,顶部137 和底部都没有算进去.
 * 3,DecorView和ContentView都是可以覆盖全部屏幕的.
 *
 */

/**
 * 模拟器结果--不隐藏状态栏:
 linearlayout frame is Rect(0, 63 - 1080, 1794)
 decoor view frame is Rect(0, 0 - 1080, 1920)
 statusBar frame is Rect(0, 0 - 1080, 63)
 navigationBar frame is Rect(0, 1794 - 1080, 1920)
 content view frame is Rect(0, 63 - 1080, 1794)
 System window manager height is 1920
 Activity window manager height is 1920
 Screen safe area frame is Rect(0, 0 - 1080, 1794)
 *
 */

/*
 * 模拟器结果--隐藏状态栏:
 * 差别就是DecorView没有StatusBar,然后中间的LinearLayout从屏幕顶部开始,也就是从0开始.
linearlayout frame is Rect(0, 0 - 1080, 1794)
screen height is 1920
decoor view frame is Rect(0, 0 - 1080, 1920)
navigationBar frame is Rect(0, 1794 - 1080, 1920)
content view frame is Rect(0, 0 - 1080, 1794)
Activity window manager height is 1920
Screen safe area frame is Rect(0, 0 - 1080, 1794)
*
* 想法:
* 1,那么要实现toast,根本不需要获取window,直接在decorView的linearlayout
 */
